package com.workcell.workdata.xthings.jobhandler;

import cn.hutool.core.date.DateUtil;
import com.workcell.workdata.xthings.service.CodeChainnumService;
import com.workcell.workdata.xthings.service.CodeChainnumUseStateDataService;
import com.workcell.workdata.xthings.service.DeviceBindService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Date;
import static cn.hutool.core.date.DatePattern.*;


/**
 * @Author guoyu
 *
 * @Date 2023/2/25 9:06
 * @Description 二维码码库定时任务调度
 */
@Component
@RequiredArgsConstructor
public class CodeChainnumJobHandler {

	private final CodeChainnumUseStateDataService useStateDataService;
	private final CodeChainnumService codeChainnumService;
	private final DeviceBindService deviceBindService;

	/**
	 * kafka串口任务发送
	 */
	@XxlJob("modbusHandler")
	public void modbusJob(){
		XxlJobHelper.log("开始执行定期发送指令任务");
		deviceBindService.sendModbusJobToKafka();
		XxlJobHelper.handleSuccess("定期发送指令任务执行完成");
	}

	/**
	 * 整点采集二维码码库设备上下线数据
	 */
	@XxlJob("collectionDeviceUseState")
	public void onTimeCollectionDeviceUseState() {
		XxlJobHelper.log("开启整点采集设备上下线数据定时任务--------"+DateUtil.format(new Date(),NORM_DATETIME_PATTERN)+"---------");
		useStateDataService.add(LocalDateTime.now());
		XxlJobHelper.handleSuccess("整点采集设备上下线数据任务执行完成--------"+DateUtil.format(new Date(),NORM_DATETIME_PATTERN)+"---------");
	}

	/**
	 * 每分钟更新二维码码库TV上下线状态
	 */
	@XxlJob("updateTvUseState")
	public void updateTvUseState() {
		XxlJobHelper.log("开启每分钟更新TV上下线状态定时任务--------"+DateUtil.format(new Date(),NORM_DATETIME_PATTERN)+"---------");
		codeChainnumService.updateUseState();
		XxlJobHelper.handleSuccess("每分钟更新TV上下线状态任务执行完成--------"+DateUtil.format(new Date(),NORM_DATETIME_PATTERN)+"---------");
	}
}
